// die Radio 1110 Datei

// hier die anderen Headers einlesen
// dann einige Sachen überschreiben

/* Zunaechst erstellen;
Initialisierung, einfaches Leseprogramm
Methoden: Init
Read a Packet, let active

Vorgehensweise:
Zunaechst nochmal Sniffer mit Uhr testen um Settings zu ermitteln
Dazu das ACC Python Programm laufen lassen um Uhr zum Straten zu bewegen
Dann Sniffer laufen lassen
Dann diese Daten in myccradio einbauen,
und einfachste Leseroutine einbauen

*/
#include <stdio.h>

#include "hal.h"
#include "cc_radio.h"

#include "mycc1110radio.h"
#include "smartrf_cc1110_chronsniff.h"


/* Main Radio Control State Machine control configuration - Calibrate when going from IDLE to RX or TX. */
#define MRFI_SETTING_MCSM0      0x14
//#define MRFI_SETTING_MCSM0      0x18

/* Main Radio Control State Machine control configuration - Go to RX state after RX
 * and go to IDLE after TX.
 */
//#define MRFI_SETTING_MCSM1      0x3C
#define MRFI_SETTING_MCSM1      0x0C
// auf null gesetzt: kein Clear channel assesment
extern volatile mytime32 rx_sfdtime;
volatile UINT32 tio;
UINT32 rfrx_tio;


void initcc1110(void) {
// startet das Radio im RX Mode
 /* internal radio register configuration */
/*
 *  Packet Length - Setting for maximum allowed packet length.
 *  The PKTLEN setting does not include the length field but maximum frame size does.
 *  Subtract length field size from maximum frame size to get value for PKTLEN.
 */
#define MRFI_SETTING_PKTLEN  200

/* Packet automation control - Original value except WHITE_DATA is extracted from SmartRF setting. */
//#define MRFI_SETTING_PKTCTRL0   (0x05 | (SMARTRF_SETTING_PKTCTRL0 & BV(6)))
// SK : needs Update !!! IST ABER OK (kein Wihietning)
#define MRFI_SETTING_PKTCTRL0   0x05 
//Doch Whitening !!
//#define MRFI_SETTING_PKTCTRL0   0x45 

#define MRFI_SETTING_PA_TABLE0 SMARTRF_SETTING_PA_TABLE0
/* TEST0 Various Test Settings - the VCO_SEL_CAL_EN bit must be zero */
#define MRFI_SETTING_TEST0      (SMARTRF_SETTING_TEST0 & ~(1))

  MCSM1     = MRFI_SETTING_MCSM1;
  MCSM0     = MRFI_SETTING_MCSM0;
  PKTLEN    = MRFI_SETTING_PKTLEN;
  PKTCTRL0  = MRFI_SETTING_PKTCTRL0;
  PA_TABLE0 = MRFI_SETTING_PA_TABLE0;
  TEST0     = MRFI_SETTING_TEST0;

  /* imported SmartRF radio register configuration */
  FSCTRL1  = SMARTRF_SETTING_FSCTRL1;
  FSCTRL0  = SMARTRF_SETTING_FSCTRL0;
  FREQ2    = SMARTRF_SETTING_FREQ2;
  FREQ1    = SMARTRF_SETTING_FREQ1;
  FREQ0    = SMARTRF_SETTING_FREQ0;
  MDMCFG4  = SMARTRF_SETTING_MDMCFG4;
  MDMCFG3  = SMARTRF_SETTING_MDMCFG3;
  MDMCFG2  = SMARTRF_SETTING_MDMCFG2;
  MDMCFG1  = SMARTRF_SETTING_MDMCFG1;
  MDMCFG0  = SMARTRF_SETTING_MDMCFG0;
  DEVIATN  = SMARTRF_SETTING_DEVIATN;
  FOCCFG   = SMARTRF_SETTING_FOCCFG;
  BSCFG    = SMARTRF_SETTING_BSCFG;
  AGCCTRL2 = SMARTRF_SETTING_AGCCTRL2;
  AGCCTRL1 = SMARTRF_SETTING_AGCCTRL1;
  AGCCTRL0 = SMARTRF_SETTING_AGCCTRL0;
  FREND1   = SMARTRF_SETTING_FREND1;
//  FREND0   = SMARTRF_SETTING_FREND0;
  FSCAL3   = SMARTRF_SETTING_FSCAL3;
  FSCAL2   = SMARTRF_SETTING_FSCAL2;
  FSCAL1   = SMARTRF_SETTING_FSCAL1;
  FSCAL0   = SMARTRF_SETTING_FSCAL0;
  TEST2    = SMARTRF_SETTING_TEST2;
  TEST1    = SMARTRF_SETTING_TEST1;

// Zustand nach Initialsierung soll erstmal "RX aktiv" sein

RFST = SRX;
//rfrx_tio = 100000;
}

#define M_SFD 0x01
#define M_RXOVF 0x40
#define M_DONE 0x10

#define RFTXRXIF RFERRIF

void setRxTIO(UINT32 aTio) {
  rfrx_tio = aTio;
}

UINT8 readcc1110Packet(UINT8* buf) {
// warten auf SFD
UINT8 len;
BITVAR readfl;
//	len = 0;
//	len = RFIF;

//	RFIF = M_SFD;		 // delete spurious SFD
//	RFIF = M_RXOVF;
//	RFIF = M_DONE;		
	len = 0;
//	return len;
	tio = rfrx_tio; // ca 4900 tics also ca 150 msec
	while (! (RFIF & M_SFD) && !(RFIF & 0xf0) && tio) 
		tio--;
	if (!tio) return 0;
	rx_sfdtime.t16 = currtime16();
	RFIF &= ~M_SFD;	
//	putchar('.');
//	return len;
// warten auf LEN

	readfl = 1;
	// in cc2430, RFTXRXIF is RFERRIF
	while (readfl) {
	  while (!RFTXRXIF && !(RFIF & 0xf0))  ; // wait for rx to occur or for error
//	  putchar('*');
	  if (RFTXRXIF) {
	    RFTXRXIF =0;
// Endabfrage fehlt !!!
	    *buf++ = RFD;
	    len++;
	    }
	  else { // some error or done
	    readfl = 0;
	    if (RFIF & 0xe0) { //some error
	      // error
		printf("\n@ERROR on RX\n");
		RFST = SIDLE;
		RFST = SRX;
//		len = -1;
		len = 0; // canged for display project, -1 is not unsigned
	       }
	    } // else some error
	  } // Readfl
	  
	RFIF = 0;		
	return len;
} 

UINT8 sendcc1110Packet(UINT8* buf, UINT8 len, UINT8 rxon) {
BITVAR errfl;

//  RFST = SIDLE;
//  mydelayms(1);
  RFIF = 0;	
  RFTXRXIF = 0;
  RFST = STX;
//  printf("1\n");
  while (!RFTXRXIF) ;
  RFTXRXIF = 0;
  RFD = len;
//  RFD = 2;
/*  
  while (!RFTXRXIF) ;
  RFTXRXIF = 0;
  RFD = 33;

  while (!RFTXRXIF) ;
  RFTXRXIF = 0;
  RFD = 44;
  
  return 0;
 */ 
  errfl = 0;
  while (!errfl && (len > 0)) {
    while (!RFTXRXIF && !(RFIF & 0xf0))  ; // wait for rx to occur or for error
    if (RFTXRXIF) {
        RFTXRXIF = 0;
	RFD = *buf++;
      } // if 
    else { // error or done
	    if (RFIF & 0xe0) { //some error
	      // error
		printf("\n@ERROR on TX\n");
		RFST = SIDLE;
		errfl = 1;
	    }
	  } // else some error   
    len--;
  } // while
 
  if (!errfl) {
    while (!(RFIF & 0xf0)) ; // auf done warten
 //   RFST = SIDLE; // warum eigentlich ???
   }
  
//  return len;
//  RFST = SIDLE;
//  mydelayus(300);  
  if (rxon) RFST = SRX;
  RFIF = 0;		

  return len;
}

volatile UINT16 currtime16(void)
{
UINT16 x1, x2;
BITVAR eaflag = EA;
  EA = 0;
  do {
  x1 = (WORTIME1 << 8); x1  |= WORTIME0; 
//  x2 = WORTIME0; x2 |= (WORTIME1 << 8);
  x2 = (WORTIME1 << 8); x2  |=  WORTIME0; 
  } while (x1 != x2);
  EA = eaflag;
return x1;
}
